package chat.aylan.top.communicate;
/**
 * 发送消息器
 */

import chat.aylan.top.commons.Information;
import com.alibaba.fastjson.JSONObject;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Sender {

    private DataOutputStream dos;

    public Sender(Socket socket){
        try {
            this.dos = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
           die();
        }
    }

    private void die(){
        try {
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void send(Information information)throws IOException {
        dos.writeUTF(JSONObject.toJSONString(information));
        dos.flush();
    }
}
